<!DOCTYPE html>
<html lang="en">

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
    <style>
        .heading td {
            background: #1D9FDF;
            color: white;
            text-align: center;
            border: none;
        }

        #heading {
            text-align: center;
        }


        table {
            border-collapse: collapse;
        }

        td {
            border: 1px solid black;
            height: 30px;
            width: 280px;
            text-align: center;
            font-weight: bold;
        }

        .noBorder td {
            border: none;
            background: lightgray;
        }

        table thead tr td {
            background: lightgray;
            height: 30px;
            min-width: 100px;
        }

        .reporttable {
            border-collapse: collapse;
        }
        #export{
            margin-left: 90px;
        }
    </style>

<script id="erasable" type="text/javascript">
   

   
    
        
        $(document).ready(function () {
            $("#export").click(function (e) {
                e.preventDefault();

                //getting data from our table
                var data_type = 'data:application/vnd.ms-excel';
                var table_div = document.getElementById('report_table');
                var table_html = table_div.outerHTML.replace(/ /g, '%20');

                var a = document.createElement('a');
                a.href = data_type + ', ' + table_html;
                a.download = 'Awards and Disbursements.xls';
                a.click();
            });
        });
        var json = "";
        var ncbc = "", district = "", sub = "";
        var checkOuLevel = function (ou) {
            $.ajax({
                async: false,
                type: "GET",
                url: "../api/organisationUnits/" + ou + ".json",
                success: function (data) {
                    json = data;

                }
            });
            return json;
        };

        var month;
        var getMonth = function (a) {
            if (a == "1" || a == "01") { month = "January" }
            if (a == "2" || a == "02") { month = "February" }
            if (a == "3" || a == "03") { month = "March" }
            if (a == "4" || a == "04") { month = "April" }
            if (a == "5" || a == "05") { month = "May" }
            if (a == "6" || a == "06") { month = "June" }
            if (a == "7" || a == "07") { month = "July" }
            if (a == "8" || a == "08") { month = "August" }
            if (a == "9" || a == "09") { month = "September" }
            if (a == "10") { month = "october" }
            if (a == "11") { month = "November" }
            if (a == "12") { month = "December" }
            return month;
        };

        var getFirstThreeColumn = function (ou) {
            $.ajax({
                async: false,
                type: "GET",
                url: "../api/organisationUnits/" + ou + ".json?fields=ancestors[id,name,level],displayName",
                success: function (data) {
                    for (var i = 0; i < data.ancestors.length; i++) {
                        if (data.ancestors[i].level == 2) {
                            district = data.ancestors[i].name;
                        }
                        if (data.ancestors[i].level == 3) {
                            sub = data.ancestors[i].name;
                        }
                        ncbc = data.displayName;

                    }

                }
            });
            return district, sub, ncbc;
        };
        var submit = function () {

            $(".reporttable tbody").remove();
            $(".reporttable tbody").detach();
            var url = window.location.href;
            var pe1 = url.split("&")[1];
            //var pe = pe1.split("=")[1];
            var valuee = $("#selection").val();
            if (valuee == "1") {
                var fyear = "Jul 2019 to Jun 2020";
            }
            else if (valuee == "2") {
                var fyear = "Jul 2018 to Jun 2019";
            }
            else if (valuee == "3") {
                var fyear = "Jul 2017 to Jun 2018";
            }
            else if (valuee == "4") {
                var fyear = "Jul 2016 to Jun 2017";
            }
            else if (valuee == "5") {
                var fyear = "Jul 2015 to Jun 2016";
            }
            // var monthCode = pe.substr(4, 6);
            // var monthh  =  getMonth(monthCode);
            // var year = pe.substr(0, 4);
            var ou1 = url.split("&")[1];
            var ou = ou1.split("=")[1];
            var ouLeveldata = checkOuLevel(ou);
            var ouLevel = ouLeveldata.level;
            var index = 1;
            var row = "<tbody>";
            //  var periodArray = getPeriodArray(pe);



            var printData = function (ou) {

                var col1 = "", col2 = "", col3 = "", col4 = "", code = "", y1 = 0, y2 = 0, y3 = 0, y4 = 0, remarks = "", total = 0;

                $.ajax({
                    async: false,
                    type: "GET",
                    dataType: "json",
                    contentType: "application/json",
                    url: "../api/26/analytics.json?dimension=pe:2015July;2016July;2017July;2018July;2019July;2020July&dimension=dx:kV08LUFhCyZ;Ny6lKDjR9OY;DAS01GaOUV3;iGDjdnCkbSQ;nehhFqejTk1&filter=ou:" + ou + "&displayProperty=SHORTNAME&outputIdScheme=NAME",
                    success: function (data) {
                        if (data.rows.length == 0) {
                            return;
                        }
                        for (var i = 0; i < data.rows.length; i++) {

                            if (data.rows[i][0] == "AD_Contract description (type)" && data.rows[i][1] == fyear) {
                                col1 = data.rows[i][2];
                            }
                            if (data.rows[i][0] == "AD_Contract code" && data.rows[i][1] == fyear) {
                                code = data.rows[i][2];
                            }
                            if (data.rows[i][0] == "AD_Modality" && data.rows[i][1] == fyear) {
                                col2 = data.rows[i][2];
                            }
                            if (data.rows[i][0] == "AD_Contracr amount" && data.rows[i][1] == fyear) {
                                col3 = data.rows[i][2];
                            }
                            if (data.rows[i][0] == "AD_Disbursement ( In reporting period)" && data.rows[i][1] == "Jul 2015 to Jun 2016") {
                                y1 = parseInt(data.rows[i][2]);
                            }
                            if (data.rows[i][0] == "AD_Disbursement ( In reporting period)" && data.rows[i][1] == "Jul 2016 to Jun 2017") {
                                y2 = parseInt(data.rows[i][2]);
                            }
                            if (data.rows[i][0] == "AD_Disbursement ( In reporting period)" && data.rows[i][1] == "Jul 2017 to Jun 2018") {
                                y3 = parseInt(data.rows[i][2]);
                            }
                            if (data.rows[i][0] == "AD_Disbursement ( In reporting period)" && data.rows[i][1] == "Jul 2018 to Jun 2019") {
                                y4 = parseInt(data.rows[i][2]);
                            }

                        }


                        $.ajax({
                            async: false,
                            type: "GET",
                            dataType: "json",
                            contentType: "application/json",
                            url: "../api/26/analytics.json?dimension=pe:2015July;2016July;2017July;2018July;2019July;2020July&dimension=dx:W9IMrHkdYQp&filter=ou:" + ou + "&displayProperty=SHORTNAME&outputIdScheme=NAME",
                            success: function (data) {
                                if (data.rows.length == 0) {
                                    return;
                                }
                                for (var i = 0; i < data.rows.length; i++) {
                                    if (data.rows[i][0] == "AD_Remark" && data.rows[i][1] == fyear) {
                                        remarks = data.rows[i][2];
                                    }

                                }
                            }
                        });




                        col4 = fyear;
                        total = y1 + y2 + y3 + y4;
                        getFirstThreeColumn(ou);
                         row = row + "<tr><td>" + district + "</td><td>" + sub + "</td><td>" + ncbc + "</td><td>" + code + "</td><td>" + col1 + "</td><td>" + col2 + "</td><td>" + parseInt(col3) + "</td><td>" + y3 + "</td><td>" + y1 + "</td><td>" + y2 + "</td><td>" + y3 + "</td><td>" + y4 + "</td><td>" + total + "</td><td>" + remarks + "</td></tr>";
                        $('.reporttable').append(row + "</tbody>");
                    }
                });
                
            };

            if (ouLevel == 1) {
                for (var p = 0; p < ouLeveldata.children.length; p++) {
                    var tempOu = ouLeveldata.children[p].id;
                    var tempOuData = checkOuLevel(tempOu);
                    if (tempOuData.children.length !== 0) {
                        for (var l = 0; l < tempOuData.children.length; l++) {
                            var tempOu2 = tempOuData.children[l].id;
                            var tempOuData2 = checkOuLevel(tempOu2);
                            if (tempOuData2.children.length !== 0) {
                                for (var m = 0; m < tempOuData2.children.length; m++) {
                                    ouu = tempOuData2.children[m].id;
                                    printData(ouu);

                                }
                            }
                        }
                    }
                }
            }

            else if (ouLevel == 2) {

                for (var p = 0; p < ouLeveldata.children.length; p++) {
                    var tempOu = ouLeveldata.children[p].id;
                    var tempOuData = checkOuLevel(tempOu);
                    if (tempOuData.children.length !== 0) {
                        for (var l = 0; l < tempOuData.children.length; l++) {
                            ouu = tempOuData.children[l].id;
                            printData(ouu);
                        }
                    }
                }
            }
            else if (ouLevel == 3) {
                for (var p = 0; p < ouLeveldata.children.length; p++) {
                    ouu = ouLeveldata.children[p].id;
                    printData(ouu);
                }
            }
            else if (ouLevel == 4) {
                printData(ou);
            }


        };
    </script>
</head>

<body>
    <div id="body">

        <button id="export">Export to Excel</button>
        <center>  <select id="selection">
                            <option value="1">Jul 2019 to Jun 2020</option>
                            <option value="2">Jul 2018 to Jun 2019</option>
                            <option value="3">Jul 2017 to Jun 2018</option>
                            <option value="4">Jul 2016 to Jun 2017</option>
                            <option value="5">Jul 2015 to Jun 2016</option>
                        </select>
                      <button onclick="submit()">submit</button></center>
        <br><br>
        <table class="table table-bordered reporttable" width="100%" id="report_table"  border='1'>
            <thead bgcolor="#1D9FDF">
                <tr class="heading" border="0" bgcolor="#1D9FDF">
                    <td colspan="3" rowspan="3"  border="0" bgcolor="#1D9FDF" ><img src="../dhis-web-commons/security/DRILP_Logo.jpg" width="120px" height="70px" ></td>
                    <td colspan="8" rowspan="1" border="0" bgcolor="#1D9FDF">
                        <p style="font-size:16px;">Earthquake Emergency Assistance Project (EEAP)</p>
                    </td>
                    <td colspan="3" rowspan="3" border="0" bgcolor="#1D9FDF"></td>
                </tr>
                <tr class="heading" border="no0ne" bgcolor="#1D9FDF">
                    <td colspan="8" rowspan="1" border="0" bgcolor="#1D9FDF">
                        <p style="font-size:20px;">Rehabilitation and Reconstruction of District Roads</p>
                    </td>
                </tr>
                <tr class="heading" border="0" bgcolor="#1D9FDF">
                    <td colspan="8" rowspan="1" border="0" bgcolor="#1D9FDF">
                        <p style="font-size:16px;">Awards and Disbursements</p>
                    </td>
                </tr>
                
                <tr>
                    <td rowspan="2" colspan="1">District</td>
                    <td rowspan="2" colspan="1">Sub-project</td>
                    <td rowspan="2" colspan="1">NCB contract</td>
                    <td rowspan="2" colspan="1">Contract code</td>
                    <td rowspan="2" colspan="1">Contact description</td>
                    <td rowspan="2" colspan="1">Modality</td>
                    <td rowspan="2" colspan="1">Contract Amount (NRs) (Exc. VAT)</td>
                    <td rowspan="1" colspan="6">Disbursement (NRs. Exc. VAT)</td>
                    <td rowspan="2" colspan="1">Remarks</td>
                </tr>
                <tr>
                    <td rowspan="1" colspan="1">In Reporting Period</td>
                    <td rowspan="1" colspan="1">FY 072/73 </td>
                    <td rowspan="1" colspan="1">FY073/74</td>
                    <td rowspan="1" colspan="1">FY 074/75</td>
                    <td rowspan="1" colspan="1">FY 075/76</td>
                    <td rowspan="1" colspan="1">Total (NRs)</td>
                </tr>
            </thead>
        </table> <br>

    </div>
</body>

</html>